﻿using System;
using System.Collections.Generic;
using System.Text;
using Crap4Net.Entities;
using NUnit.Framework;
using Crap4Net;

namespace Crap4Net.UnitTests.EntitiesTests
{
    [TestFixture]
    public class CrapStatsEntryTest
    {
        readonly static MethodSignature _method1 = new MethodSignature("dll", "ns", "type1", "method1", new string[] { });
        readonly static MethodSignature _method2 = new MethodSignature("dll", "ns", "type1", "method2", new string[] { });
        readonly static MethodSignature _method3 = new MethodSignature("dll", "ns", "type1", "method3", new string[] { });

        private List<CombinedDataEntry> _crapMethodData = new List<CombinedDataEntry>
                                           {
                                               new CombinedDataEntry(_method1, 100, 30),
                                               new CombinedDataEntry(_method2, 100, 20),
                                               new CombinedDataEntry(_method3, 100, 10)
                                           };

        [Test]
        public void TotalCrapIsCalculatedAsTheSumOfCrapForEachMethod()
        {
            var crapSummary = new CrapStatsEntry(_crapMethodData, 1);
            Assert.AreEqual(60, crapSummary.CalculateTotalCrap());
        }

        [Test]
        public void MethodCountIsCalculatedAsTheSumOfMethods()
        {
            var crapSummary = new CrapStatsEntry(_crapMethodData, 2);
            Assert.AreEqual(3, crapSummary.CalculateTotalMethods());
        }

        [Test]
        public void CrapMethodCountIsCalculatedAsTheSumOfMethodsWithCrapScoreAboveThreshold()
        {
            var crapSummary = new CrapStatsEntry(_crapMethodData, 25);
            Assert.AreEqual(1, crapSummary.CalculateTotalCrapMethods());
        }

        [Test]
        public void CrapMethodPercentIsCalculatedAsTheNumCrapMethodsOverNumMethods()
        {
            var crapSummary = new CrapStatsEntry(_crapMethodData, 25);
            Assert.AreEqual(33.3d, crapSummary.CalculateCrapMethodPercentage(),0.1d);
        }

        [Test]
        public void CrapMethodPercentIs0WhenThereIsNoCrapMethodData()
        {
            var crapSummary = new CrapStatsEntry(new List<CombinedDataEntry>(), 2);
            Assert.AreEqual(0d, crapSummary.CalculateCrapMethodPercentage());
        }


        [Test]
        public void TotalCrapLoadIsCalculatedAsTheSumOfCrapLoadForEachMethod()
        {
            var crapSummary = new CrapStatsEntry(_crapMethodData, 1);
            var actual = crapSummary.CalculateTotalCrapLoad();
            Assert.AreEqual(52.8d, actual,0.1);
        }
    }
}